import MathUtils from "./MathUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class Test extends cc.Component {
@property(cc.Node)
lightNode = null;
@property(sp.Skeleton)
panda = null;
@property(cc.Node)
btn2 = null;

targetPos = null;
tweenAc = null;
moveTime = 0;
move_path_arr = [];
move_to_next = 0;
move_speed = 1000;
vx = 0;
vy = 0;
move_total_time = 0;
move_time = 0;
can_move = true;

start() {
    this.scheduleOnce(() => {
        this.panda.setAnimation(0, "idle_1", false);
    }, 3);

    this.scheduleOnce(() => {
        this.panda.setAnimation(0, "Unhappy", false);
    }, 5);

    this.scheduleOnce(() => {
        this.panda.setAnimation(0, "idle_1", false);
    }, 8);
}

btnTest1() {
    console.error("btnTest1");
    this.scheduleOnce(() => {
        this.btn2.removeFromParent();
    });
}

async Test1() {
    for (let e = 0; e < 3; e++) {
        await new Promise((resolve) => {
            this.scheduleOnce(() => {
                resolve(null);
                console.log("xxxxxxxxx2", e);
            }, 2);
        });

        console.log("xxxxxxxxx", e);
    }

    console.log("循环走完了~~~");
}

move() { }

async moving() {
    for (let e = 1; e < 4; e++) {
        await this.scheduleOnce((e) => {
            console.log("mooving" + e);
        }, e);

        console.log("到这了", e);
    }
}

async itemMove(e, t, a) {
    const o = Date.now();
    const r = this.node.position;
    const n = cc.v3(e, t);
    let s = 0;
    
    while (s < a) {
        this.node.position = this.Ve3Lerp(r, cc.v3(n.x, n.y), s / a);
        console.log("posy----", this.node.y);
        await new Promise((resolve) => {
            this.scheduleOnce(() => {
                resolve(null);
            });
        });

        s += 1 / 60;
    }
    const i = Date.now();
    console.log("多长时间？？？", i - o);
    this.node.position = cc.v3(n.x, n.y);
}

Ve3Lerp(e, t, a) {
    return new cc.Vec3(e.x + (t.x - e.x) * a, e.y + (t.y - e.y) * a);
}

tweenAction() {
    if (this.tweenAc) {
        this.tweenAc.then(cc.tween(this.node).by(.2, { y: -100 }));
    } else {
        this.tweenAc = cc.tween(this.node).by(.2, { y: -100 }).start();
    }
}

start_move() {
    console.log("start----", this.move_to_next, this.move_path_arr.length);

    if (this.move_to_next >= this.move_path_arr.length) {
        this.can_move = false;
        console.log("移动完成", this.node.x, this.node.y);
        this.node.position = this.move_path_arr[this.move_path_arr.length - 1];
    } else {
        const e = this.node.position;
        const t = this.move_path_arr[this.move_to_next].sub(e);
        const a = t.mag();
        this.vx = this.move_speed * t.x / a;
        this.vy = this.move_speed * t.y / a;
        this.move_total_time = a / this.move_speed;
        this.move_time = 0;
    }
}

move_update(e) {
    if (this.can_move) {
        this.move_time += e;
        if (this.move_time > this.move_total_time) {
            e -= this.move_time - this.move_total_time;
        }

        const t = this.vx * e;
        const a = this.vy * e;
        this.node.x += t;
        this.node.y += a;

        if (this.move_time >= this.move_total_time) {
            this.move_to_next++;
            this.start_move();
        }
    }
}

update() { }

tweenTest() {
    this.tween.start();
}

async tweenTest2() {
    await new Promise((resolve, reject) => {
        cc.tween(this.node)
            .to(.1, { y: -100 })
            .call(() => {
                resolve(null);
                console.log("call");
            })
            .start();
    }).catch(() => { });

    console.log("wancheng");
}

lineTest() {
    const e = cc.v2(100, -100);
    const t = cc.v2(200, -200);
    console.error("lineTest----", MathUtils.extendedLinePoint(e, t, 10));
    console.error("lineTest2----", MathUtils.extendedLinePoint(t, e, 10));

    const a = cc.v2(100, -200);
    const o = cc.v2(200, -100);
    console.error("lineTest----", MathUtils.extendedLinePoint(a, o, 10));
    console.error("lineTest2----", MathUtils.extendedLinePoint(o, a, 10));
}

tewwnTest() {
    cc.tween(this.node)
        .to(1, { position: cc.v3(0, 200) }, { easing: "backInOut" })
        .start();
}
}